/*
 * $Id: crule.h 108 2008-08-14 01:39:48Z dtikhonov $
 *
 * crule.h -- coloring rules.
 */

#ifndef _DHEXVIEW_CRULE_H_ /* [ */
#define _DHEXVIEW_CRULE_H_ 1

#include <stdbool.h>
#include <stdint.h>

typedef enum {
    CRULE_TYPE_ABSOLUTE = 1,
    CRULE_TYPE_PERIODIC,
    CRULE_TYPE_EXACT,
    CRULE_TYPE_REGEX,
} crule_type_t;

typedef struct {
    int matrix[8][8]; // Indexed by COLOR_BLACK through COLOR_WHITE
    int npairs;
} color_pairs_t;

struct crule {
    crule_type_t    (*type)(struct crule *self);

    int             (*level)(struct crule *self);

    void            (*set_level)(struct crule *self, int level);

    void            (*set_position)(struct crule *self, uint64_t pos);

    void            (*set_exact_string)(
                        struct crule *self
                      , const uint8_t *str
                      , int sz
                    );

    bool            (*set_regex)(
                        struct crule *self
                      , const char *regex
                      , int len
                      , int flags
                    );

    bool            (*add_color)(
                        struct crule *self
                      , int start_range   // can be positive or negative
                      , int sz            // zero means size of match
                      , int fg_color
                      , int bg_color // -1 means default (current) background
                    );

    void            (*init_color_pairs)(
                        struct crule *self
                      , color_pairs_t *color_pairs
                    );

    /* Returns number of times rule applied */
    int             (*colorize)(
                        struct crule *self
                      , uint64_t current_position
                      , const unsigned char *buf
                      , int sz
                      , int *color_buffer
                    );

    bool            (*is_initialized)(struct crule *self);

    void            (*destroy)(struct crule *self);

    void *ctx;
};
typedef struct crule crule_t;

crule_t *
crule_new (crule_type_t type);

struct crulist {
    bool        (*add)(struct crulist *self, crule_t *crule);

    int         (*nrules)(struct crulist *self);

    int         (*colorize)(
                    struct crulist *self
                  , uint64_t current_position
                  , const unsigned char *buf
                  , int sz
                  , int *color_buffer
                );

    void        (*init_colors)(
                    struct crulist *self
                  , color_pairs_t* color_pairs
                );

    void        (*destroy_rules)(struct crulist *self);

    void        (*destroy)(struct crulist *self);

    void       *ctx;
};
typedef struct crulist crulist_t;

crulist_t *
crulist_new (void);

#endif /* ] _DHEXVIEW_CRULE_H_ */
